#ifndef _CRANE_JPUC_DECODER_H_
#define _CRANE_JPUC_DECODER_H_

#ifdef __cplusplus
extern "C" {
#endif

#include "lv_drv_conf.h"

#if USE_CRANE_JPU

#define CRANE_JPEG_DECODE_PASS   1
#define CRANE_JPEG_DECODE_FAIL   0

#define JPEG_QTABLE_NUM  4
#define JPEG_QTABLE_SIZE 64
#define JPEG_HUFF_TABLE_LENGTH_SIZE  16
#define JPEG_HUFF_TABLE_DC_VALUE_SIZE   12
#define JPEG_HUFF_TABLE_AC_VALUE_SIZE   162
#define JPEG_HUFF_TABLE_DC_SIZE   (JPEG_HUFF_TABLE_LENGTH_SIZE + JPEG_HUFF_TABLE_DC_VALUE_SIZE)
#define JPEG_HUFF_TABLE_AC_SIZE   (JPEG_HUFF_TABLE_LENGTH_SIZE + JPEG_HUFF_TABLE_AC_VALUE_SIZE)

enum {
    ROTATE_0 = 0,
    ROTATE_90,
    ROTATE_180,
    ROTATE_270
};

enum {
    MIRROR_NONE = 0,
    MIRROR_VERTICAL,
    MIRROR_HORIZONTAL,
    MIRROR_BOTH,
};

typedef struct jpu_decode_config_s {
    uint8_t rotate_mode;
    uint8_t mirror_mode;
} jpu_decode_config_t;

typedef struct jpeg_header_info_s {
    uint16_t width;
    uint16_t height;
    uint32_t sos_end_position;
    uint8_t  qtable_got;
    uint8_t  qtable[JPEG_QTABLE_NUM][JPEG_QTABLE_SIZE];
    uint8_t huff_is_default;
    uint8_t huff_got;
    uint8_t huff_table_dc_lum[JPEG_HUFF_TABLE_DC_SIZE];
    uint8_t huff_table_dc_chrom[JPEG_HUFF_TABLE_DC_SIZE];
    uint8_t huff_table_ac_lum[JPEG_HUFF_TABLE_AC_SIZE];
    uint8_t huff_table_ac_chrom[JPEG_HUFF_TABLE_AC_SIZE];
    uint8_t huff_table_dc_lum_length;
    uint8_t huff_table_dc_chrom_length;
    uint8_t huff_table_ac_lum_length;
    uint8_t huff_table_ac_chrom_length;
} jpeg_header_info_t;

uint8_t jpeg_header_parser(uint8_t *data, uint32_t data_length, jpeg_header_info_t *header_info);
uint8_t jpu_decode(uint8_t *jpeg_data, uint32_t data_size, jpeg_header_info_t *header_info,
            uint8_t *dest_buf_yuv, uint8_t *dest_buf_rgb565, uint16_t dest_width, uint16_t dest_height);

#endif /* USE_CRANE_JPU */

#ifdef __cplusplus
} /* extern "C" */
#endif

#endif

